/** 笔函数
 * @ 标  识: hchhIOT
 * @ 作  者: 阿拉丁矿工 黄传鸿 <hchhyzc@foxmail.com>
 */

/* ================================================================================================ */
// 🍀引入
/* ================================================================================================ */
import axios from "axios"
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// 🐸 图元端函数引入
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
import {
  onRecordLog,
} from "@/functions";
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// 🐸 引入存储
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
import { storeToRefs } from "pinia";
import { useEditorStore } from "@/store"; // 画布

/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// 🐸 使用存储
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
const myStore = useEditorStore(); // 画布


/* ================================================================================================ */
//                                函数
/* ================================================================================================ */

/** 选择当前笔筒 */
export const onSelectedPenContainer = (penContainer: any) => {
 // console.log("★ 笔函数_选择当前笔筒")
  /** 日志处理器 */
  onRecordLog("选择笔筒 " + penContainer.label)
 // console.log(penContainer);
 // console.log(Object.keys(penContainer.children).length)
  let long = Object.keys(penContainer.children).length
  if (long !== 0) {
    myStore.selectedPenContainer = penContainer;//选择的笔筒
    myStore.selectedPen = myStore.selectedPenContainer.children[0]//选择的画笔
    let tempCopy = JSON.parse(JSON.stringify(myStore.rightNavigation))
    tempCopy.forEach((subItem: any) => {
      if (subItem.key === 'Pen') {
        subItem.frameActive = true
      } else {
        subItem.frameActive = false
      }
    })
    myStore.rightNavigation = tempCopy
  }
}

/** 选择当前画笔 */
export const onSelectedPen = (selectedPen: any) => {
 // console.log("★ 笔函数_选择当前画笔");

 // console.log(selectedPen);
  myStore.selectedPen = selectedPen;//选择的画笔
  myStore.penSelectState = 2;
  myStore.actionRightNavigation("RightPen"); //激活右框导航
  /** 日志处理器 */
  onRecordLog("选择当前画笔 " + selectedPen.label);
}

/** 下载笔筒 */
export const onPenContainerGet = () => {
 // console.log("㊣ 公共函数-获取工程列表");
  myStore.penSelectState = 0;// /** 笔选择状态-0无-1笔筒-2笔-3已下载-4上载  */
  /** ★ 发送-工程数据 */
  axios({
    url: `/api/HCIOT?query=getPenContainer`,
    method: 'get',
  })
    .then((response: any) => {
     // console.log(response.data);
      if (response.status === 200) {
        myStore.penContainer = response.data;
        /** 设置选择的笔桶 */
        myStore.selectedPenContainer = myStore.penContainer[0];
        /** 设置选择的画笔 */
        myStore.selectedPen = myStore.penContainer[0].children[0];
        /** 笔选择状态-0无-1笔筒-2笔-3已下载-4上载 */
        myStore.penSelectState = 2;

        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        // 🐸 提示 记录到日志
        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        onRecordLog("获取笔筒成功");
      }
      else {
        onRecordLog("获取笔筒失败");
      }
    })
    .catch((error: any) => {
     // console.log("故障");
      onRecordLog("获取笔筒失败");
    });

}

/** 上载笔筒 */
export const onPenContainerSave = () => {
 // console.log("㊣ 公共函数-保存工程");

  /** ★ 发送-工程数据 */
  axios({
    url: `/api/HCIOT?query=savePenContainer`,
    method: 'post',
    headers: { 'content-type': 'application/json' },
    data: myStore.penContainer,
  })
    .then((response: any) => {
     // console.log(response.data);
      if (response.status === 200) {
        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        // 🐸 提示 记录到日志
        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        onRecordLog("上载笔筒成功");
      }
      else {
        onRecordLog("上载笔筒失败");
      }
    })
    .catch((error: any) => {
     // console.log("故障");
      onRecordLog("上载笔筒失败");
    });
}

/** 新建笔筒 */
export const onPenContainerAdd = () => {
 // console.log("笔函数-新建笔筒");
  /** 计数 */
  let PenContainerCont = onSetElementCont(
    "PenContainerCont",
    Number(onGetElementCont("PenContainerCont")) + 1
  );
  /** 笔筒 */
  let NewPenContainer = {
    label: '笔筒' + PenContainerCont,
    key: 'penContainer' + onGetUUID(),
    type: 'penContainer',
    children: []
  }
  myStore.penContainer.push(NewPenContainer);
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  // 🐸 提示 记录到日志
  /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  onRecordLog("新建笔筒");
}

/** 删除笔筒 */
export const onPenContainerDelete = (key: string) => {
 // console.log("笔函数-删除笔筒");
  /** 笔筒 */
  let penContainer = myStore.penContainer;
  /** 选择的笔筒 */
  let selectedPenContainer = myStore.selectedPenContainer;
  /** 判断选择笔筒 */
  if (key) {
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    // 🐸 遍历-全部笔筒 
    /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    for (let index = 0; index < penContainer.length; index++) {
      /** 笔筒项 */
      let PenContainerItem = penContainer[index];
      if ((PenContainerItem.key === selectedPenContainer.key) && (PenContainerItem.key !== 'CustomPenContainer')) {
        penContainer.splice(index, 1);//删除笔筒
        myStore.selectedPenContainer = {}
        myStore.penSelectState = 3;// 笔选择状态：0无;1笔筒;2笔-;3已下载;4上载;
        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        // 🐸 提示 记录到日志
        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        onRecordLog("笔筒已经删除");
        return;
      } else {
        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        // 🐸 提示 记录到日志
        /**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
        onRecordLog("笔筒不能删除，笔可以删除");

      }
    }
  } else {

  }

}

/** 剪切笔 */
export const onPenCut = (key: string) => {
 // console.log("笔函数-剪切笔");

  /**深度拷贝-笔筒*/
  let penContainer = JSON.parse(JSON.stringify(myStore.penContainer));
  /** 判断选择笔 */
  if (key) {
    /** 复制笔 */
    myStore.copyPen = JSON.parse(JSON.stringify(myStore.selectedPen));
    let NewKey = 'Pen' + onGetUUID();
    myStore.copyPen.key = NewKey;
   // console.log(myStore.copyPen);
    /** 遍历-全部笔筒*/
    for (let indexA = 0; indexA < penContainer.length; indexA++) {
      /** 笔筒项*/
      let PenContainerItem = penContainer[indexA];
      /** 笔筒列表*/
      let PenArry = PenContainerItem.children;
      /** 遍历-笔筒列表 */
      for (let indexB = 0; indexB < PenArry.length; indexB++) {
        /** 笔*/
        let Pen = PenArry[indexB];
        if (Pen.key === myStore.selectedPen.key) {
          PenArry.splice(indexB, 1);//删除笔
          myStore.selectedPen = {}//清理选中笔
          myStore.penSelectState = 3;// /** 笔选择状态-0无-1笔筒-2笔-3已下载 */
        }
      }
    }
    /** 转储-笔筒 */
    myStore.penContainer = penContainer;

    onRecordLog("剪切笔成功");
  } else {
    onRecordLog("没有选择")
    myStore.copyPen = {}
  }
}

/** 复制笔 */
export const onPenCopy = () => {
 // console.log("笔函数-复制笔");
  /** 选择笔 */
  let selectedPen = myStore.selectedPen;
  /** 判断选择 */
  if (selectedPen.key) {
    /** 复制笔 */
    myStore.copyPen = JSON.parse(JSON.stringify(myStore.selectedPen));
    let NewKey = 'Pen' + onGetUUID();
    myStore.copyPen.key = NewKey;
   // console.log(myStore.copyPen );
    /** 日志处理器 */
    onRecordLog("复制笔成功");
  } else {
    onRecordLog("没有选择");
    myStore.copyPen = {}
  }
}

/** 插入笔 */
export const onPenInsert = () => {
 // console.log("笔函数-插入笔");

  /**深度拷贝-笔筒*/
  let penContainer = JSON.parse(JSON.stringify(myStore.penContainer));
  /** 判断复制笔 */
  if (myStore.copyPen.key) {
    /** 复制笔 */
    let copyPen = myStore.copyPen;
    /** 遍历-全部笔筒 */
    for (let PenContainerIndex = 0; PenContainerIndex < penContainer.length; PenContainerIndex++) {
      /** 笔筒项*/
      let PenContainerItem = penContainer[PenContainerIndex];
      /** 笔筒列表*/
      let PenArry = PenContainerItem.children;
      /** 判断快包键 */
      if (myStore.selectedPenContainer.key === PenContainerItem.key) {
        PenArry.push(copyPen);
        myStore.copyPen = {}
      }
    }
    /** 转储-笔筒 */
    myStore.penContainer = penContainer;
    onRecordLog("插入笔成功");

  } else {
    onRecordLog("没有笔");
  }
}

/** 删除笔 */
export const onPenDelete = (key: string) => {
 // console.log("笔函数-删除笔");

  /** 深度拷贝-笔筒 */
  let penContainer = JSON.parse(JSON.stringify(myStore.penContainer));
  /** 选择的笔 */
  let selectedPen = myStore.selectedPen;
  /** 判断选择 */
  if (selectedPen.key) {
    /** 遍历-笔筒 */
    for (let indexA = 0; indexA < penContainer.length; indexA++) {
      let PenContaineItem = penContainer[indexA];
      let PenArry = PenContaineItem.children;
      /** 遍历-笔 */
      for (let indexB = 0; indexB < PenArry.length; indexB++) {
        let PenItem = PenArry[indexB];
        /** 判断-选中的画笔 */
        if (PenItem.key === key) {
          PenArry.splice(indexB, 1);
          myStore.selectedPen = {}
        }
      }
    }
    /** 转储-笔筒 */
    myStore.penContainer = penContainer;
    myStore.penSelectState = 0;

    onRecordLog("删除笔成功");
  } else {
    onRecordLog("没有选择");
  }
}

/** 新建笔 */
export const onCreatePen = (key: string) => {
 // console.log("笔函数-建笔");


  /** 计数 */
  let PenCont = onSetElementCont(
    "PenCont",
    Number(onGetElementCont("PenCont")) + 1
  );
  /**深度拷贝-笔筒*/
  let penContainer = JSON.parse(JSON.stringify(myStore.penContainer));
  /** 新笔键 */
  let NewKey = 'Pen' + onGetUUID();
  /** 新笔 */
  let NewPen = {
    "label": "笔_" + PenCont,//笔名称
    "key": NewKey,//笔键
    "stroke": "rgb(0,0,225)",// 轮廓颜色
    "stroke_width": 5,// 轮廓宽度
    "stroke_dasharray": "",//虚线
    "stroke_dashoffset": 0,// 线虚实偏移
    "stroke_linecap": "round",//端点样式
    "fill": "rgba(255, 255, 255, 0)",// 填充颜色
    "opacity": 1,// 透明度_元素
    "fill_opacity": 1,// 透明度_填充
    "stroke_opacity": 1// 透明度_轮廓
  }
 // console.log(NewPen);
  if (key) {
    /** 遍历-全部笔筒 */
    for (let PenContainerIndex = 0; PenContainerIndex < penContainer.length; PenContainerIndex++) {
      /** 笔筒项*/
      let PenContainerItem = penContainer[PenContainerIndex];
      /** 笔筒列表*/
      let PenArry = PenContainerItem.children;
      /** 判断笔筒键 */
      if (key === PenContainerItem.key) {
        PenArry.push(NewPen);
      }
    }
    /** 转储-笔筒 */
    myStore.penContainer = penContainer;
    /** 设置-选择的笔筒*/
    myStore.penContainer.forEach((item: any) => {
      if (item.key === key) {
        myStore.selectedPenContainer = item;//选择的笔筒
      }
    })
    /** 设置-选择的画笔*/
    myStore.selectedPen = NewPen;
    onRecordLog("新建笔");
  } else {
    onRecordLog("没有选笔筒");
  }
}
